<script setup lang="ts">
const name = "Login"

import {getNowTime} from "@/utils/time";
import {ElNotification} from "element-plus"
import type {FormInstance, FormRules} from 'element-plus'
import {ref, onMounted, reactive} from "vue"
import router from "@/router";
import {usePiniaStore} from "@/store"
// import {get, post} from "@/api/methods"

// 2 拿到实例
const piniaStore = usePiniaStore();


onMounted(() => {
  const isLogin = JSON.parse(localStorage.getItem('isLogin'))
  if (isLogin) {
    piniaStore.login()
  }
})

/**
 * 表单验证规则
 */
interface RuleForm {
  name: string
  password: string
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: '',
  password: '',
})
const rules = reactive<FormRules<RuleForm>>({
  name: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 18, message: '输入长度在 3 ~ 18', trigger: 'blur'},
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur',
    },
    {min: 6, max: 18, message: '输入长度在 6 ~ 18', trigger: 'blur'},
  ]
})

/**
 * 表单提交方法
 * @param formEl
 */
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // console.log('submit!')
      login()
      console.log("ruleForm.name", ruleForm.name)
      console.log("ruleForm.password", ruleForm.password)
    } else {
      // console.log('error submit!', fields)
      ElNotification({
        title: '输入信息有误',
        message: getNowTime(),
        type: 'error',
      })

    }
  })
}


const login = () => {
  console.log('login');
  piniaStore.login()
  // post('api/user/login', {
  //   username: ruleForm.name,
  //   password: ruleForm.password
  // }).then(res => {
  //   console.log(res)
  // })

}

const register = () => {
  router.push("/register")
}

</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">欢迎登录</h2>
      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          size="default"
          status-icon
      >
        <el-form-item prop="name">
          <el-input v-model="ruleForm.name" placeholder="Enter Your Username" clearable/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" placeholder="Enter Your Password" clearable show-password/>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="submitForm(ruleFormRef)" class="login-button">Log in</el-button>
      <hr>
      <el-button style="margin-top: 15px;" type="primary" @click="register" class="login-button">Register</el-button>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-box {
  width: 400px;
  padding: 40px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
}

.login-input {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
}
</style>